package homework4;

import java.util.Calendar;
import java.util.Date;

public final class DateHelper {

	private DateHelper() {
		
	}
	
	public static DateHelper getInstance() {
		return new DateHelper();
	}
	
	public Date toDate( int year , int month , int date , int hourOfDay , int minute ) {
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(year,month,date,hourOfDay,minute,0);
		calendar.set(Calendar.MILLISECOND,0);
		return calendar.getTime();
	}
	
	public Date toDate( int year , int month , int date , int hourOfDay , int minute , int second ) {
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(year,month,date,hourOfDay,minute,second);
		calendar.set(Calendar.MILLISECOND,0);
		return calendar.getTime();
	}

	public Date toDate( int year , int month , int date ,int hourOfDay , int minute , int second , int millis ) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year,month,date,hourOfDay,minute,second);
		calendar.set(Calendar.MILLISECOND,millis);
		return calendar.getTime();
		
	}
	
	public String toString( final Calendar date ) {

		StringBuilder builder = new StringBuilder();
		
		int year = date.get(Calendar.YEAR);
		builder.append( year + "" );
		
		int month = date.get( Calendar.MONTH ) + 1 ;
		if(month<10) {
			builder.append('0');
		}
		builder.append( month + "" );
		
		int dat = date.get( Calendar.DATE );
		if(dat<10) {
			builder.append('0');
		}
		builder.append( dat + "" );
		
		int hours = date.get( Calendar.HOUR_OF_DAY );
		if(hours<10) {
			builder.append('0');
		}
		builder.append( hours + ":" );
		
		int minutes = date.get( Calendar.MINUTE );
		if(minutes<10) {
			builder.append('0');
		}
		builder.append( minutes + ":" );
		
		int seconds = date.get( Calendar.SECOND );
		if(seconds<10) {
			builder.append('0');
		}
		builder.append( seconds + "." );
		
		int millis = date.get( Calendar.MILLISECOND );
		if(seconds<10) {
			builder.append("00");
		}else if(seconds<100) {
			builder.append("0");
		}
		builder.append( millis );
		
		String str = builder.toString();
		return str;
	}
	
	public String toString( final Date date ) {
		
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.setTime(date);
		return toString(calendar);
	}
	
	public int durationOfDays( Date first , Date second ) {
	
		long ms = 1000L*60*60*24;
		System.out.println("ms:"+ms);
		long diff = first.getTime()-second.getTime();
		System.out.println("diff:"+diff);
		return (int)(diff/ms);
	}
	
	
}